﻿//-----------------------------------------------------------------------
// <copyright>
//     Copyright (c) Artur Mustafin. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Math.Compiler.Expressions
{
    using System.Collections.Generic;
    using System;
    using System.Linq.Expressions;

    public abstract class MathExpression
    {
        private readonly List<MathExpression> _children;

        public abstract double Eval();

        public Expression<Func<double>> Expression()
        {
            return () => Eval();
        }

        internal void Add(MathExpression expression)
        {
            _children.Add(expression);
        }

        internal void AddRange(IEnumerable<MathExpression> expressions)
        {
            _children.AddRange(expressions);
        }

        protected MathExpression()
        {
            _children = new List<MathExpression>();
        }

        internal IEnumerable<MathExpression> Children
        {
            get
            {
                return _children;
            }
        }

        public static implicit operator double(MathExpression value)
        {
            return value.Eval();
        }

        public static implicit operator Func<double>(MathExpression value)
        {
            return value.Expression().Compile();
        }
    }
}
